<?php

class User_Subscribe {
    public static function user_activation($uid,$email,$mail_type='tourist'){
        $account = User_Account::user_account_info($uid)->current();
        //保存用户激活token
        $data['mailToken'] = md5($account['created'].time());
        $data['token_exptime'] = time()+60*60*24;
        $data['email'] = $email;
        $data['active'] = 0;
        User_Account::update($data,'uid',$uid);
        //发送邮件

        if($mail_type == 'guider'){
            $email_body = '邮件激活地址：'.url::site("/guider/confirm/?token=".$data['mailToken'], 'http', FALSE).' 如果链接不能直接打开，请复制到浏览器中打开';
        }else if($mail_type == 'tourist'){
            $email_body = '邮件激活地址：'.url::site("/tourist/confirm/?token=".$data['mailToken'], 'http', FALSE).' 如果链接不能直接打开，请复制到浏览器中打开';
        }else{
            $email_body = '邮件激活地址：'.url::site("/homepage/confirm/?token=".$data['mailToken'], 'http', FALSE).' 如果链接不能直接打开，请复制到浏览器中打开';
        }

        $status = notice::factory('email')
            ->to($email)
//                ->to($post['email'])
            ->sender('哇吖旅行')
            ->subject("用户激活")
            ->message($email_body)
            ->send();
        $data['status'] = $status;

        $match_qq = "#@qq\.com#i";
        $match_163 = "#@163\.com#i";
        $match_waya = "#@wayatrip\.com#i";
        $match_sina = "#@sina\.com.*#i";
        $match_sohu = "#@sohu\.com.*#i";
        $match_126 = "#@126\.com.*#i";
        $match_hotmail = "#@hotmail\.com.*#i";
        $match_outlook = "#@outlook\.com.*#i";
        $match_yahoo = "#@yahoo\.com.*#i";
        $match_gmail = "#@gmail\.com.*#i";
        $match_189 = "#@189\.cn.*#i";
        $match_139 = "#@139\.com.*#i";
        $match_tom = "#@tom\.com.*#i";
//        $rs = ;

        if(preg_match($match_qq,$email)){
            $data['url'] = "qq";
        }
        elseif(preg_match($match_163,$email)){
            $data['url'] = "163";
        }
        elseif(preg_match($match_waya,$email)){
            $data['url'] = "waya";
        }
        elseif(preg_match($match_sina,$email)){
        	$data['url'] = "sina";
        }
        elseif(preg_match($match_sohu,$email)){
        	$data['url'] = "sohu";
        }
        elseif(preg_match($match_126,$email)){
        	$data['url'] = "126";
        }
        elseif(preg_match($match_hotmail,$email)||preg_match($match_outlook,$email)){
        	$data['url'] = "hot";
        }
        elseif(preg_match($match_yahoo,$email)){
        	$data['url'] = "yahoo";
        }
        elseif(preg_match($match_gmail,$email)){
        	$data['url'] = "gmail";
        }
        elseif(preg_match($match_189,$email)){
        	$data['url'] = "189";
        }
        elseif(preg_match($match_139,$email)){
        	$data['url'] = "139";
        }
        elseif(preg_match($match_tom,$email)){
        	$data['url'] = "tom";
        }
        
        else{
            $data['url'] = "";
        }

        return $data;
    }

    public static function confirm_token($token)
    {
        $data = User_Account::account_checkout('mailToken', $token)->current();
        if (empty($data)) {
            $msg = "验证失败";
            return array(false,$msg);
        } else {
            if ($data['active'] == 1) {
                $msg = "验证完成，无须再次验证";
                $data['msg'] = $msg;
                return array(true,$data);
            } else {
                if (time() > $data['token_exptime']) {
                    $msg = "邮件过期";
                    return array(false,$msg);
                } else {
                    $msg = "验证通过";
                    $save['active'] = 1;
                    $save['userId'] = $data['email'];
                    User_Account::update($save, 'mailToken', $token);
                    $data['msg'] = $msg;
                    return array(true,$data);
                }
            }
        }
    }


    public static function post_email($email,$title,$body){
        $status = notice::factory('email')
            ->to($email)
//                ->to($post['email'])
            ->sender("哇吖旅行")
            ->subject($title)
            ->message($body)
            ->send();

        return $status;
    }
}
